package com.witframework.meta;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.javalid.annotations.validation.MaxLength;

import com.witframework.core.annotations.Wit;
import com.witframework.util.Assert;
/**
 * 实体的元数据描述类，封装了类信息
 * @author wuxinyang
 *
 */


@Entity @Table(name="Meta_Entity") @Wit(cnname = "实体",cnproperty="displayName")
public class MetaEntity extends BaseMetaObject{	

	@NotNull @MaxLength(length=255) @Wit(cnname="实体类的类名", description="实体类的限定类名")
	private String className;

	
	@Column(length=100) @MaxLength(length=50)@Wit(cnname="实体的打印名称", description="定义实体的显示名称")
	private String displayName;
	
	@Column(length=100) @MaxLength(length=50)@Wit(cnname="显示属性", description="显示名称")
	private String cnproperty;
	
	@Column(length=127) @MaxLength(length=60)	@Wit(cnname="实体的表名", description="定义对应的表名，默认值：实体的短类名")
	private String tableName;

	@Column(length=10) @Wit(cnname="版本化标志",description="enum('versionNum','') 添加对乐观锁定的支持")
	private String hasVersion="versionNum";
	
	@Version @Wit(cnname="版本",description="版本")
	protected int versionNum;

	@Column(length=1)@Wit(cnname="锁定?",description="是否锁定,锁定态的不允许通过平台工具修改")
	private String islocked;
	
	@Column(length=32)@Wit(cnname="分类",description="所属模块")
	private String module;
	
	@OneToMany(mappedBy="metaEntity",cascade=CascadeType.ALL)@LazyCollection(value=LazyCollectionOption.FALSE)
	@Wit(cnname="属性列表", description="实体类的属性列表")
	private List<MetaEntityField> fields;
	
	public MetaEntity(){
		fields=new ArrayList<MetaEntityField>();
	}
	/**
	 * 构造方法
	 * @param classFullName 类全名称
	 */
	public MetaEntity(String classFullName){
		this();
		Assert.isEmpty(classFullName, "实体类的类名不能为空。");
		this.className=classFullName;
		MetaEntityField field=new MetaEntityField("id",EntityFieldType.STRING,"id");
		field.setIsIdentity("Y");
		field.setLength(32);
		fields.add(field);
	}
	public String getWitAnnotations(){
		String annotation=" @Wit(";
		if(getDisplayName()!=null){			
			annotation+="cnname=\""+getDisplayName()+"\"";
		}
		if(getCnproperty()!=null){
			if(annotation.length()>6) annotation+=",";
			annotation+="cnproperty=\""+getCnproperty()+"\"";
		}
		if(getDescription()!=null){
			if(annotation.length()>6) annotation+=",";
			annotation+="description=\""+getDescription()+"\"";
		}
		return annotation+")";
	}

	/**
	 * 返回类的包名称
	 * @return 包名称
	 */
	public String getPackageName(){
		return className.substring(0,className.lastIndexOf("."));
	}
	/**
	 * 返回类的简短名称（即不包含包名称）
	 * @return 类短名称
	 */
	public String getSimpleClassName(){
		return className.substring(className.lastIndexOf(".")+1);
	}
	/**
	 * 添加类字段定义
	 * @param field EntityField
	 */
	public void addField(MetaEntityField field){
		if(!fields.contains(field))
			fields.add(field);
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getDisplayName() {
		return displayName;
	}
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}	
	
	public String getHasVersion() {
		return hasVersion;
	}
	public void setHasVersion(String hasVersion) {
		this.hasVersion = hasVersion;
	}
	
	public void setFields(List<MetaEntityField> fields) {
		this.fields = fields;
	}
	/**
	 * 获取类字段信息
	 * @return List
	 */
	public List<MetaEntityField> getFields() {
		return fields;
	}
	public String getTableName() {
		if(tableName==null)
			return className.substring(className.lastIndexOf(".")+1);
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	
	public String getIslocked() {
		return islocked;
	}
	public void setIslocked(String islocked) {
		this.islocked = islocked;
	}
	
	
	
	public String getModule() {
		return module;
	}
	public void setModule(String module) {
		this.module = module;
	}	
	
	public int getVersionNum() {
		return versionNum;
	}
	public void setVersionNum(int versionNum) {
		this.versionNum = versionNum;
	}
	public String getCnproperty() {
		return cnproperty;
	}
	public void setCnproperty(String cnproperty) {
		this.cnproperty = cnproperty;
	}	
}
